using System;
using Slobby.Server.Net;

namespace Slobby.Server {
    /// <summary>
    /// Listens for clients and authenticates them
    /// </summary>
    public class ClientAuthenticator {
        Authentication.AuthenticationProvider authProvider;

        public event Action<Client> ClientConnected;

        public void Process(ClientConnection connection) {
            bool waitingfordetails = true;
            while (waitingfordetails) {
                object o = connection.RecieveMessage();
                if (o.GetType() == typeof(SharedNetworkTypes.TryAuthenticationMessage)) {
                    SharedNetworkTypes.TryAuthenticationMessage m = (SharedNetworkTypes.TryAuthenticationMessage) o;
                    connection.SendMessage(new SharedNetworkTypes.PassedAuthenticationMessage());
                    ClientConnected(new Client(connection, m.UserName));
                    break;
                }
            }
        }

        public ClientAuthenticator(Authentication.AuthenticationProvider provider) {
            authProvider = provider;
        }
    }
}
